package com.ssy.lingxi.purchase.serviceimpl.inner;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.purchase.api.dto.request.RequisitionQuantityDeductReq;
import com.ssy.lingxi.purchase.api.dto.request.UpdateCreditPointRequest;
import com.ssy.lingxi.purchase.entity.PurchaseProduct;
import com.ssy.lingxi.purchase.entity.PurchaseRequisition;
import com.ssy.lingxi.purchase.entity.purchaseinquiry.PurchaseInquiry;
import com.ssy.lingxi.purchase.repository.PurchaseInquiryRepository;
import com.ssy.lingxi.purchase.repository.PurchaseRequisitionRepository;
import com.ssy.lingxi.purchase.service.inner.PurchaseInnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 采购询价、采购招标、采购竞价 - 内部接口 - 业务实现类
 * @author fhj
 * @date 2021/07/09
 * @version 2.0.0
 * */
@Slf4j
@Service
public class PurchaseInnerServiceImpl implements PurchaseInnerService {

    @Resource
    private PurchaseInquiryRepository purchaseInquiryRepository;
    @Resource
    private PurchaseRequisitionRepository purchaseRequisitionRepository;


    /**
     * 根据会员ID和角色ID更新信用分
     * 注: 会员的信用积分每次有变动都会通知该接口实时更新采购询价、采购招标、采购竞价对应的信用分
     * */
    @Override
    public Wrapper<?> updateCreditPoint(UpdateCreditPointRequest request) {
        //根据会员ID和角色ID获取采购需求单
        List<PurchaseInquiry> purchaseInquiryList =  purchaseInquiryRepository.findByCreateMemberIdAndCreateMemberRoleId(
                request.getMemberId(), request.getRoleId()
        );
        //批量更新采购需求单的信用积分
        if(CollectionUtil.isNotEmpty(purchaseInquiryList)) {
            purchaseInquiryRepository.saveAll(
                    purchaseInquiryList.stream().peek(a ->
                            a.setIntegral(request.getCreditPoint())
                    ).collect(Collectors.toList())
            );
        }

        return Wrapper.success();
    }

    /**
     * 请购单转请购订单扣减请购数量、增加已转订单数量内部接口
     * @param req 请求参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> requisitionQuantityDeduct(RequisitionQuantityDeductReq req) {
        PurchaseRequisition requisition = purchaseRequisitionRepository.findById(req.getRequisitionId()).orElse(null);
        if (requisition == null) {
            return Wrapper.fail(ResponseCode.EXCEL_UPLOAD_ERROR_NULL);
        }
        //已转订单总数量
        BigDecimal totalTransQuantity = BigDecimal.ZERO;

        for (PurchaseProduct product : requisition.getProducts()) {
            req.getProducts().stream().filter(productReq -> product.getProductId().equals(productReq.getProductId())).findFirst()
                    //增加单个已转订单数量
                    .ifPresent(productVO -> product.setTransferQuantity(product.getTransferQuantity().add(productVO.getTransferQuantity())));
            //统计已转订单总数量
            totalTransQuantity = totalTransQuantity.add(product.getTransferQuantity());
        }
        //已转订单总数量
        requisition.setTransferQuantity(totalTransQuantity);

        purchaseRequisitionRepository.save(requisition);
        return Wrapper.success();
    }
}
